import 'dart:convert';
import 'dart:math';

import 'package:flutter_app/Model/Enums.dart';
import 'package:flutter_app/Model/UserModel.dart';
import 'package:flutter_yfree/main/YFree+Model.dart';
import 'package:flutter_yim/YIM.dart';

class ConversationModel {
  bool isMine = false;
  String conversationID = "";
  String avatar = "";
  String name = "";
  String content = "";
  int unReadCount = 0;
  YIMEnum_MessageState messageState = YIMEnum_MessageState.UnKnown;
  Enum_MessageType messageType = Enum_MessageType.UnKnown;
  String date = "";

  ConversationModel.fromYIMConversation(YIMConversation conversation, bool isMine) {
    String addZero(int value) => "${value < 10 ? "0$value" : value}";

    String getMessageStateText() {
      if (!isMine) return "";
      switch (conversation.lastMessage!.messageState) {
        case YIMEnum_MessageState.Sending:
          return "[发送中] ";
        case YIMEnum_MessageState.Success_Read:
          return "[已读] ";
        case YIMEnum_MessageState.Success_UnRead:
          return "[未读] ";
        case YIMEnum_MessageState.Fail:
          return "[发送失败] ";
        default:
          return "[未知状态] ";
      }
    }

    String getMessageContentText(Enum_MessageType messageType, String content) {
      switch (messageType) {
        case Enum_MessageType.Text:
          return content;
        default:
          return "[${messageType.title}]";
      }
    }

    final originMessageModel = OriginMessageModel.fromYIMMessage(conversation.lastMessage!);
    this.isMine = isMine;
    conversationID = isMine ? conversation.lastMessage!.to : conversation.lastMessage!.from;
    avatar = isMine ? originMessageModel.avatar_to : originMessageModel.avatar_from;
    name = isMine ? originMessageModel.name_to : originMessageModel.name_from;
    if (conversation.lastMessage!.isRevoke) {
      content = "${isMine ? "您" : "对方"}撤回了一条消息";
    } else {
      content = "${getMessageStateText()}${getMessageContentText(originMessageModel.type, originMessageModel.content)}";
    }
    messageType = originMessageModel.type;
    date = "${conversation.lastMessage!.time.year}-${addZero(conversation.lastMessage!.time.month)}-${addZero(conversation.lastMessage!.time.day)} ${addZero(conversation.lastMessage!.time.hour)}:${addZero(conversation.lastMessage!.time.minute)}";
    messageState = conversation.lastMessage!.messageState;
    unReadCount = conversation.unRead;
  }
}

class MessageModel {
  bool isMine = false;
  String id = "";
  String avatar = "";
  String name = "";
  String content = "";
  YIMEnum_MessageState messageState = YIMEnum_MessageState.UnKnown;
  Enum_MessageType messageType = Enum_MessageType.UnKnown;
  int date = 0;
  bool isRevoke = false;
  bool isVoicePlayed = false;

  double progress_upload = 0; //文件上传进度，0～100，负数为上传失败
  String videoThumbnailPath = ""; //视频消息上传中的本地封面文件路径
  bool isVoicePlaying = false; //语音消息是否在播放

  MessageModel.fromYIMMessage(YIMMessage message, bool isMine) {
    final originMessageModel = OriginMessageModel.fromYIMMessage(message);
    this.isMine = isMine;
    this.id = message.id;
    this.avatar = originMessageModel.avatar_from;
    this.name = originMessageModel.name_from;
    this.content = originMessageModel.content;
    this.messageState = message.messageState;
    this.messageType = originMessageModel.type;
    this.date = message.time.millisecondsSinceEpoch;
    this.isRevoke = message.isRevoke;
    final localExt = message.localExt == null ? {} : message.localExt as Map<dynamic, dynamic>;
    isVoicePlayed = messageType == Enum_MessageType.Voice && (localExt["isVoicePlayed"] ?? false);
  }

  MessageModel.fromUpload({
    required UserModel userModel,
    required this.content,
    required this.messageType,
  }) {
    this.isMine = true;
    this.id = "${DateTime.now().microsecondsSinceEpoch}_${Random().nextInt(999999)}";
    this.avatar = userModel.avatar;
    this.name = userModel.name;
    this.content = content;
    this.messageState = YIMEnum_MessageState.Sending;
    this.messageType = messageType;
    this.date = DateTime.now().millisecondsSinceEpoch;
    this.isRevoke = false;
  }
}

class OriginMessageModel {
  OriginMessageModel();

  late String avatar_from;
  late String avatar_to;
  late String name_from;
  late String name_to;
  late Enum_MessageType type;
  late String content;

  OriginMessageModel.fromYIMMessage(YIMMessage message) {
    Map<dynamic, dynamic> content = json.decode(json.decode(message.content));
    this.avatar_from = content.byString("avatar_from", defaultValue: "")!;
    this.avatar_to = content.byString("avatar_to", defaultValue: "")!;
    this.name_from = content.byString("name_from", defaultValue: "")!;
    this.name_to = content.byString("name_to", defaultValue: "")!;
    this.type = content.byEnum("type", (rawValue) => Enum_MessageType.from(rawValue), defaultValue: Enum_MessageType.UnKnown)!;
    this.content = content.byString("content", defaultValue: "")!;
  }

  String toJson() => json.encode({
        "avatar_from": avatar_from,
        "avatar_to": avatar_to,
        "name_from": name_from,
        "name_to": name_to,
        "type": type.type,
        "content": content,
      });
}
